import './assets/bootstrap/css/bootstrap.min.css';
import './assets/bootstrap/js/bootstrap.min.js';
import './common/js/sha1.min.js';
import utils from './common/js/utils'

import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import qs from 'qs';
import VueRouter from 'vue-router';

import App from './components/App.vue';
import Login from './components/login/login.vue';
import schoolInfo from './components/school-info/school-info.vue';
import studentFileList from './components/student-file-list/student-file-list';
import addStudentFile from './components/add-student-file/add-student-file';
import studentPlan from './components/student-plan/student-plan';
import planSelection from './components/plan-selection/plan-selection';
// 状态管理
Vue.use(Vuex);
// 路由

// Vue.prototype.HOST = 'http://118.31.37.164:8080/';
Vue.prototype.HOST = 'api/';
Vue.prototype.$utils = utils;
axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.options.root = '/';
// axios.headers.common['Authorization'] = 'Basic YXBpOnBhc3N3b3Jk';
axios.options.emulateJSON = true;
Vue.prototype.$http = axios;
Vue.prototype.$qs = qs;
Vue.use(VueRouter);

// 请求配置
axios.interceptors.request.use(config => {
  // loading
  return config
}, error => {
  return Promise.reject(error)
});

axios.interceptors.response.use(response => {
  return response
}, error => {
  return Promise.resolve(error.response)
});

// 路由配置
// 如果需要加菜单，就在这里添加路由，并在UserMenu.vue添加入口router-link
const router = new VueRouter({
  routes: [{
    path: '/login',
    component: Login
  }, {
    path: '/schoolInfo',
    component: schoolInfo
  }, {
    path: '/studentFileList',
    component: studentFileList
  }, {
    path: '/addStudentFile',
    component: addStudentFile
  }, {
    path: '/studentPlan',
    component: studentPlan
  }, {
    path: '/planSelection',
    component: planSelection
  },]
});

// Vuex配置
const store = new Vuex.Store({
  state: {
    domain: 'http://test.example.com', // 保存后台请求的地址，修改时方便（比方说从测试服改成正式服域名）
    userInfo: { // 保存用户信息
      nick: null,
      ulevel: null,
      uid: null,
      portrait: null
    }
  },
  mutations: {
    // 更新用户信息
    updateUserInfo(state, newUserInfo) {
      state.userInfo = newUserInfo;
    }
  }
});

// 设置cookie,增加到vue实例方便全局调用
// vue全局调用的理由是，有些组件所用到的接口可能需要session验证，session从cookie获取
// 当然，如果session保存到vuex的话除外
Vue.prototype.setCookie = (c_name, value, expiredays) => {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = c_name + '=' + escape(value) + ((expiredays == null) ? '' : ';expires=' + exdate.toGMTString());
};

// 获取cookie、
function getCookie(name) {
  var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)');
  var arr = document.cookie.match(reg);
  if (arr) {
    return (arr[2]);
  } else {
    return null;
  }
}

Vue.prototype.getCookie = getCookie;

// 删除cookie
Vue.prototype.delCookie = (name) => {
  var exp = new Date();
  exp.setTime(exp.getTime() - 1);
  var cval = getCookie(name);
  if (cval != null) {
    document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString();
  }
};

// vue实例
var app = new Vue({
  data: {},
  el: '#app',
  render: h => h(App),
  router,
  store,
  watch: {
    '$route': 'checkLogin'
  },
  created() {
    this.checkLogin();
  },
  methods: {
    checkLogin() {
      // 检查是否存在session
      if (!this.getCookie('session')) {
        this.$router.push('/login');
      }
      // else {
      //   this.$router.push('/schoolInfo');
      // }
    }
  }
});
